﻿using System.Collections.Generic;
using System.Web.UI.WebControls;
using PBOffice.Models;
using PBOffice.BLL;
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class MeetingRoomManager_CheckMeetingRoom : System.Web.UI.Page
{
    List<MeetingRoom> room = new MeetingRoomManager().GetAllMeetingRoom();
    protected override void OnInit(EventArgs e)
    {
        TemplateField customField = new TemplateField();

        customField.ShowHeader = true;
        customField.HeaderTemplate = new GridViewTempate(DataControlRowType.Header, "动态添加列");
        GridViewTempate gvt = new GridViewTempate(DataControlRowType.DataRow, "lbtn", "Name");
        gvt.eh += new GridViewTempate.EventHandler(lbtn_Click);
        customField.ItemTemplate = gvt;
        GridView1.Columns.Add(customField);
        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           // GridView1.DataSource = getDateTable();
            GridView1.DataBind();
        }
    }

    public void lbtn_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(GetType(), "test", "alert('ok');", true);
    }
}   

public class GridViewTempate : ITemplate
{

    public delegate void EventHandler(object sender, EventArgs e);
    public event EventHandler eh;

    private DataControlRowType templateType;

    private string columnName;
    private string controlID;    

    public GridViewTempate(DataControlRowType type, string colname)   

    {   

        templateType = type;   

        columnName = colname;   

    }
    public GridViewTempate(DataControlRowType type, string controlID, string colname)   
    {   
        templateType = type;   
        this.controlID = controlID;   
        columnName = colname;   
    }   


    public void InstantiateIn(System.Web.UI.Control container)
    {
        switch (templateType)
        {
            case DataControlRowType.Header:
                Literal lc = new Literal();
                lc.Text = columnName;
                container.Controls.Add(lc);
                break;
            case DataControlRowType.DataRow:
                LinkButton lbtn = new LinkButton();
                lbtn.ID = this.controlID;
                if (eh != null)
                {
                    lbtn.Click += new System.EventHandler(eh);
                }
                lbtn.DataBinding += new System.EventHandler(lbtn_DataBinding);

                container.Controls.Add(lbtn);

                break;
            default:
                break;
        }
    }
    void lbtn_DataBinding(object sender, EventArgs e)
    {
        LinkButton lbtn = sender as LinkButton;
        if (lbtn != null)
        {
            GridViewRow container = lbtn.NamingContainer as GridViewRow;
            if (container != null)
            {
                object dataValue = DataBinder.Eval(container.DataItem, columnName);
                if (dataValue != DBNull.Value)
                {
                    lbtn.Text = dataValue.ToString();
                }
            }
        }


    }
}